package com.swak.frame.util;

import java.io.Serializable;
import java.util.Properties;

/**
 * ClassName: UUIDHexGenerator.java 
 * @Description: UUID生产策略 IP+JVM+TIME>>32+TIME+COUNT
 * @author colley.ma
 * @date 2021年3月15日
 */
public class UUIDHexGenerator extends AbstractUUIDGenerator {
	public static final UUIDHexGenerator DEFAULT = new UUIDHexGenerator();
	private String sep = "";

	protected String format(final int intval) {
		final String formatted = Integer.toHexString(intval);
		final StringBuilder buf = new StringBuilder("00000000");
		buf.replace(8 - formatted.length(), 8, formatted);
		return buf.toString();
	}

	protected String format(final short shortval) {
		final String formatted = Integer.toHexString(shortval);
		final StringBuilder buf = new StringBuilder("0000");
		buf.replace(4 - formatted.length(), 4, formatted);
		return buf.toString();
	}

	public Serializable generate(final Object obj) {
		return new StringBuilder(36).append(format(getIP())).append(sep).append(format(getJVM())).append(sep)
				.append(format(getHiTime())).append(sep).append(format(getLoTime())).append(sep)
				.append(format(getCount())).toString();
	}

	public void configure(final Properties params) {
		sep = params.getProperty("separator", "");
	}

	public static final String generator() {
		return String.valueOf(UUIDHexGenerator.DEFAULT.generate(null));
	}

	public static final String generator(final Object obj) {
		return String.valueOf(UUIDHexGenerator.DEFAULT.generate(obj));
	}
	
	public static void main(String[] args) {
		System.out.println(generator());
	}
}

